'use strict'

const util = require('../../util/util')
const { randomString } = util

const Service = require('egg').Service
class UserService extends Service {
  // 获取用户信息
  async get() {
    const { ctx } = this
    const result = await ctx.model.User.find({})
    return result
  }

  // 新增用户信息
  async add() {
    const { ctx } = this
    return await ctx.model.User.create({
      userName: ctx.request.body.userName ? ctx.request.body.userName : randomString(),
      createDate: new Date(),
      updtaeDate: new Date(),
    })
  }

  async edit() {
    const { ctx } = this
    if (ctx.request.body.id) {
      return await ctx.model.User.updateOne(
        { _id: ctx.request.body.id },
        {
          userName: ctx.request.body.userName ? ctx.request.body.userName : randomString(),
          createDate: ctx.request.body.createDate ? ctx.request.body.createDate : new Date(),
          updtaeDate: new Date(),
        },
      )
    } else {
      return await { msg: '请输入id' }
    }
  }

  async delete() {
    const { ctx } = this
    if (ctx.request.body.id) {
      return await ctx.model.User.deleteOne(
        { _id: ctx.request.body.id },
      )
    } else {
      return await { msg: '请输入id' }
    }
  }
}
module.exports = UserService
